<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/17
 * Time: 22:28
 */
namespace App\Admin\Traits;

use App\Models\Roles;
use App\Models\RoleUser;
use Encore\Admin\Admin;

trait UserTrait {
    public function getUserId()
    {
       return $this->getUser()->id();
    }

    public function getUser()
    {
        return \Encore\Admin\Facades\Admin::guard();
    }

    public function isAdministrator()
    {
        $uid = $this->getUserId();
        $role = RoleUser::query()->where("user_id", $uid)->first();
        if (!$role) return false;
        $role_id = $role->role_id;
        return !is_null(Roles::query()->where("id", $role_id)
            ->where("slug", "administrator")
            ->first());
    }
}